﻿using Common;
using DataLibrary;
using System;
using System.Linq;
using System.Windows.Forms;

namespace TicketSystem
{
    public partial class Form_GuideUpdate : Form
    {
        public Form_GuideUpdate()
        {
            InitializeComponent();
        }

        public string GuideCard;
        private TicketSystemEntities tse = new TicketSystemEntities();
        private PublicMethod pm = new PublicMethod();
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (pm.IsTelephone(this.txtPhone.Text))
            {
                if (pm.IsAccountNumber(this.txtIdCard.Text))
                {
                    string guidcard = GuideCard;
                    var guide = tse.Guides.Where(c => c.GuideCard == GuideCard).FirstOrDefault();
                    
                    guide.DepartmentID = 1;
                    guide.Name = this.txtName.Text;
                    guide.Sex = this.comSex.SelectedItem.ToString();
                    guide.NickName = this.txtNickName.Text;
                    guide.IDCard = this.txtIdCard.Text;
                    guide.Phone = this.txtPhone.Text;
                    guide.GuideCard = this.txtGuideCard.Text;
                    tse.Configuration.ValidateOnSaveEnabled = false;
                    try
                    {
                        if (tse.SaveChanges() >= 1)
                        {
                            MessageBox.Show("修改成功");
                        }
                        else
                        {
                            MessageBox.Show("修改失败");
                        }
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        return;
                    }

                }
                else
                {
                    MessageBox.Show("身份证号输入有误");
                }
            }
            else
            {
                MessageBox.Show("手机号输入有误");
            }
        }

        private void Form_GuideUpdate_Load(object sender, EventArgs e)
        {
            string guidcard = GuideCard;
            var guide = tse.Guides.Where(c => c.GuideCard == GuideCard).FirstOrDefault();
            this.txtName.Text = guide.Name;
            this.txtNickName.Text = guide.NickName;
            this.comSex.Text = guide.Sex;
            this.txtIdCard.Text = guide.IDCard;
            this.txtPhone.Text = guide.Phone;
            this.txtGuideCard.Text = guide.GuideCard;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
    }
}
